<script>
import 'common/transferPlatformTool.js';
/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
export default {

  onLaunch ( option ) {
    // 国际化，设置当前语言
    if ( this.vuex_locale ) {
      this.$i18n.locale = this.vuex_locale;
      this.$u.api.lang( {
        lang: this.vuex_locale
      } );
    }

    // 易政网单点登录
    if ( option && option.query.token ) {
      uni.showLoading( {
        title: '加载中'
      } );
      this.checkLogin( option.query.token );
    } else {
      this.loginwzt();
    }
    /*
     * 设置底部导航栏角标
     * uni.setTabBarBadge({
     *   index: 0,
     *   text: '3'
     * });
     * uni.removeTabBarBadge({
     *   index: 0
     * });
     */
  },
  onShow ( option ) {
    // 安卓调用前端返回
    window.goBackRootVC = function () {
      let a = window.location.href;
      let temp = a.slice( a.indexOf( 'pages' ) );

      if ( temp == 'pages/sys/msg/index2' || temp == 'pages/sys/user/index' ||
        temp == 'pages/sys/home/index' || a.indexOf( 'backShow' ) != -1 ) {
        // 安卓页面的关闭
        fly.request( {
          action: 'BaseComponents.appClick',
          args: ['back']
        } );
      } else {
        // 返回页面
        uni.navigateBack();
      }
    };
  },
  methods: {
    // 易政网单点登录
    async checkLogin ( token ) {
      if ( token !== '' && token !== null && token != undefined ) {
        // 过滤token中的特殊值
        token = token.replace( '#/', '' );
        const tokenInfo = await this.$u.api.ssoToken( {
          token: token
        } );
        let params = {
          username: tokenInfo.username,
          token: tokenInfo.token,
          url: ''
        };
        // 拿到token 调用免登接口
        const res = await this.$u.api.sso( params );

        this.$u.toast( res.message || '未连接服务器' );
        if ( res.result == 'true' ) {
          setTimeout( () => {
            uni.hideLoading();
            uni.reLaunch( {
              url: '/pages/sys/home/index?backShow=1'
            } );
          }, 500 );
        }
        if ( res.isValidCodeLogin ) {
          this.isValidCodeLogin = true;
          this.refreshImgValidCode();
        }
      } else {
        uni.reLaunch( {
          url: '/pages/sys/home/index'
        } );
      }
    },
    async loginwzt () {
      let that = this;


      /*
       * TODO 跨域问题需要解决
       * const info = await this.$u.api.wztGetUser(params);
       */

      croods.customPlugin( {
        action: 'UserPlugin.login',
        params: {},
        success: async function ( data ) {
          let params = {
            appId: 'wzt',
            tokenId: data.token,
            remoteIp: data.moapIp,
            attributeNames: 'loginName,userid'
          };
          const info = await that.$u.api.wztGetUser( params );
          let params2 = {
            username: info.username,
            token: info.token,
            url: ''
          };
          // 拿到token 调用免登接口
          const res = await that.$u.api.sso( params2 );

          if ( res.result == 'true' ) {
            setTimeout( () => {
              uni.hideLoading();
              uni.reLaunch( {
                url: '/pages/sys/home/index?backShow=1'
              } );
            }, 500 );
          } else {
            setTimeout( () => {
              uni.hideLoading();
              uni.reLaunch( {
                url: '/pages/sys/login/errorIndex'
              } );
            }, 500 );
          }

          if ( res.isValidCodeLogin ) {
            this.isValidCodeLogin = true;
            this.refreshImgValidCode();
          }
        }
      } );
    }
  }
};
</script>
<style lang="scss">
@import "uview-ui/index.scss";
@import "pages/common/jeesite.scss";
</style>